package org.imlinker.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
 
import org.imlinker.core.Core;
import org.imlinker.plugin.PluginManager;
import org.imlinker.util.StringUtils;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;

@SuppressWarnings("serial")
public class PreferenceWindow extends javax.swing.JDialog {

	/**
	 * 生成新的配置窗口
	 */
	public PreferenceWindow(final XMPPConnection connection,
			final PluginManager paraPluginManager, final java.awt.Frame parent) {
		super(parent, true);

		this.connection = connection;
		this.pluginManager = paraPluginManager;

		initComponents();

		initialValues();

		setLocationRelativeTo(null);

		addWindowListener(new WindowAdapter() {
			public void windowClosed(final WindowEvent e) {
				PreferenceWindow.this.dispose();
			}
		});
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		JTabbedPane jTabbedPane1 = new javax.swing.JTabbedPane();
		JPanel jPanel1 = new javax.swing.JPanel();
		JPanel jPanel7 = new javax.swing.JPanel();
		JLabel jLabel1 = new javax.swing.JLabel();
		userIdField = new javax.swing.JTextField();
		JPanel jPanel2 = new javax.swing.JPanel();
		JPanel jPanel4 = new javax.swing.JPanel();
		JLabel jLabel3 = new javax.swing.JLabel();
		addBuddyField = new javax.swing.JTextField();
		addButton = new javax.swing.JButton();
		JPanel jPanel5 = new javax.swing.JPanel();
		JLabel jLabel4 = new javax.swing.JLabel();
		deleteBuddyBox = new javax.swing.JComboBox();
		deleteButton = new javax.swing.JButton();
		JPanel jPanel3 = new javax.swing.JPanel();
		JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
		pluginList = new javax.swing.JList();
		JPanel jPanel6 = new javax.swing.JPanel();
		JLabel jLabel6 = new javax.swing.JLabel();
		pluginNameLabel = new javax.swing.JLabel();
		pluginCheckBox = new javax.swing.JCheckBox();
		closeButton = new javax.swing.JButton();

		java.util.ResourceBundle bundle = java.util.ResourceBundle
				.getBundle("org/imlinker/gui/Bundle"); // NOI18N

		setTitle(bundle.getString("PreferenceWindow.title"));

		jPanel7.setBorder(javax.swing.BorderFactory
				.createTitledBorder("User info"));

		jLabel1.setText("UserID:");

		String user = connection.getUser();
		user = user.substring(0, user.indexOf("/"));
		userIdField.setText(user);
		userIdField.setEditable(false);

		{
			javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(
					jPanel7);
			jPanel7.setLayout(jPanel7Layout);
			jPanel7Layout
					.setHorizontalGroup(jPanel7Layout
							.createParallelGroup(
									javax.swing.GroupLayout.Alignment.LEADING)
							.addGroup(
									jPanel7Layout
											.createSequentialGroup()
											.addGroup(
													jPanel7Layout
															.createParallelGroup(
																	javax.swing.GroupLayout.Alignment.LEADING)
															.addGroup(
																	jPanel7Layout
																			.createSequentialGroup()
																			.addGap(
																					41,
																					41,
																					41)
																			.addComponent(
																					jLabel1))
															.addGroup(
																	jPanel7Layout
																			.createSequentialGroup()
																			.addContainerGap()))
											.addPreferredGap(
													javax.swing.LayoutStyle.ComponentPlacement.RELATED)
											.addGroup(
													jPanel7Layout
															.createParallelGroup(
																	javax.swing.GroupLayout.Alignment.LEADING)
															.addComponent(
																	userIdField,
																	javax.swing.GroupLayout.PREFERRED_SIZE,
																	249,
																	javax.swing.GroupLayout.PREFERRED_SIZE))
											.addContainerGap()));
			jPanel7Layout
					.setVerticalGroup(jPanel7Layout
							.createParallelGroup(
									javax.swing.GroupLayout.Alignment.LEADING)
							.addGroup(
									jPanel7Layout
											.createSequentialGroup()
											.addGroup(
													jPanel7Layout
															.createParallelGroup(
																	javax.swing.GroupLayout.Alignment.BASELINE)
															.addComponent(
																	jLabel1)
															.addComponent(
																	userIdField,
																	javax.swing.GroupLayout.PREFERRED_SIZE,
																	javax.swing.GroupLayout.DEFAULT_SIZE,
																	javax.swing.GroupLayout.PREFERRED_SIZE))
											.addGap(18, 18, 18)
											.addGroup(
													jPanel7Layout
															.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE))
											.addContainerGap(37,
													Short.MAX_VALUE)));

			javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
					jPanel1);
			jPanel1.setLayout(jPanel1Layout);
			jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
					javax.swing.GroupLayout.Alignment.LEADING).addGroup(
					javax.swing.GroupLayout.Alignment.TRAILING,
					jPanel1Layout.createSequentialGroup().addContainerGap()
							.addComponent(jPanel7,
									javax.swing.GroupLayout.DEFAULT_SIZE,
									javax.swing.GroupLayout.DEFAULT_SIZE,
									Short.MAX_VALUE).addContainerGap()));
			jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
					javax.swing.GroupLayout.Alignment.LEADING).addGroup(
					jPanel1Layout.createSequentialGroup().addContainerGap()
							.addComponent(jPanel7,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									javax.swing.GroupLayout.DEFAULT_SIZE,
									javax.swing.GroupLayout.PREFERRED_SIZE)
							.addContainerGap(66, Short.MAX_VALUE)));
		}

		jTabbedPane1.addTab(
				bundle.getString("PreferenceWindow.basicTab.title"), jPanel1);

		jPanel4.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Add buddy"));

		jLabel3.setText("ID:");

		addButton.setText("Add");

		javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(
				jPanel4);
		jPanel4.setLayout(jPanel4Layout);
		jPanel4Layout.setHorizontalGroup(jPanel4Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jPanel4Layout.createSequentialGroup().addContainerGap()
						.addComponent(jLabel3).addGap(18, 18, 18).addComponent(
								addBuddyField,
								javax.swing.GroupLayout.DEFAULT_SIZE, 217,
								Short.MAX_VALUE).addGap(18, 18, 18)
						.addComponent(addButton,
								javax.swing.GroupLayout.PREFERRED_SIZE, 67,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap()));
		jPanel4Layout
				.setVerticalGroup(jPanel4Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel4Layout
										.createSequentialGroup()
										.addGroup(
												jPanel4Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jLabel3,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																25,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(addButton)
														.addComponent(
																addBuddyField,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		jPanel5.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Delete buddy"));

		jLabel4.setText("ID:");

		DefaultComboBoxModel model = new DefaultComboBoxModel();
		Collection<RosterEntry> collect = connection.getRoster().getEntries();
		for (RosterEntry entry : collect) {
			model.addElement(entry.getUser());
		}
		deleteBuddyBox.setModel(model);

		deleteButton.setText("Delete");

		javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(
				jPanel5);
		jPanel5.setLayout(jPanel5Layout);
		jPanel5Layout.setHorizontalGroup(jPanel5Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jPanel5Layout.createSequentialGroup().addContainerGap()
						.addComponent(jLabel4).addGap(18, 18, 18).addComponent(
								deleteBuddyBox,
								javax.swing.GroupLayout.PREFERRED_SIZE, 216,
								javax.swing.GroupLayout.PREFERRED_SIZE).addGap(
								18, 18, 18).addComponent(deleteButton,
								javax.swing.GroupLayout.DEFAULT_SIZE, 68,
								Short.MAX_VALUE).addContainerGap()));
		jPanel5Layout
				.setVerticalGroup(jPanel5Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel5Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel5Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel4)
														.addComponent(
																deleteBuddyBox,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																deleteButton))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
				jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout
				.setHorizontalGroup(jPanel2Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								jPanel2Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addComponent(
																jPanel5,
																javax.swing.GroupLayout.Alignment.LEADING,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																jPanel4,
																javax.swing.GroupLayout.Alignment.LEADING,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE))
										.addContainerGap()));
		jPanel2Layout
				.setVerticalGroup(jPanel2Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel2Layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(
												jPanel4,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jPanel5,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addContainerGap(48, Short.MAX_VALUE)));

		jTabbedPane1.addTab(
				bundle.getString("PreferenceWindow.buddyTab.title"), jPanel2);

		pluginList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		jScrollPane1.setViewportView(pluginList);

		jPanel6.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Plugin Info"));

		jLabel6.setText("Name:");

		pluginCheckBox.setText("Enable");

		javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(
				jPanel6);
		jPanel6.setLayout(jPanel6Layout);
		jPanel6Layout
				.setHorizontalGroup(jPanel6Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel6Layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(jLabel6)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												jPanel6Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																pluginCheckBox)
														.addComponent(
																pluginNameLabel,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																156,
																Short.MAX_VALUE))
										.addContainerGap()));
		jPanel6Layout.setVerticalGroup(jPanel6Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jPanel6Layout.createSequentialGroup().addGroup(
						jPanel6Layout.createParallelGroup(
								javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(jLabel6).addComponent(
										pluginNameLabel)).addGap(18, 18, 18)
						.addComponent(pluginCheckBox).addContainerGap(106,
								Short.MAX_VALUE)));

		javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(
				jPanel3);
		jPanel3.setLayout(jPanel3Layout);
		jPanel3Layout
				.setHorizontalGroup(jPanel3Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel3Layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(
												jScrollPane1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												114,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jPanel6,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE).addGap(18, 18,
												18)));
		jPanel3Layout
				.setVerticalGroup(jPanel3Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								jPanel3Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel3Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addComponent(
																jPanel6,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																jScrollPane1,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																192,
																Short.MAX_VALUE))
										.addContainerGap()));

		jTabbedPane1.addTab(bundle
				.getString("PreferenceWindow.pluginTab.title"), jPanel3);

		closeButton.setText("         Close        ");
		closeButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				PreferenceWindow.this.setVisible(false);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 405,
				Short.MAX_VALUE).addGroup(
				javax.swing.GroupLayout.Alignment.TRAILING,
				layout.createSequentialGroup().addContainerGap(278,
						Short.MAX_VALUE).addComponent(closeButton)
						.addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addComponent(
												jTabbedPane1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												253,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE).addComponent(
												closeButton).addContainerGap()));

		pack();
	}// </editor-fold>

	private void initialValues() {
		final DefaultListModel listModel = new DefaultListModel();
		ArrayList<String> list = pluginManager.getNameList();
		for (String name : list) {
			listModel.addElement(name);
		}
		pluginList.setModel(listModel);

		// 插件列表的监听，每选择一个插件，修改右侧的状态显示
		pluginList.addListSelectionListener(new ListSelectionListener() {

			public void valueChanged(final ListSelectionEvent e) {
				// 当值修改完毕
				if (e.getValueIsAdjusting()) {
					return;
				}

				int i = e.getFirstIndex();
				// 没有选择值时跳出
				if (i < 0) {
					return;
				}

				String item = (String) listModel.getElementAt(i);
				pluginNameLabel.setText(item);

				pluginCheckBox.setSelected(Boolean.getBoolean(Core
						.getValue("plugin." + item + ".able")));
			}
		});

		// 插件的状态设置
		pluginCheckBox.addItemListener(new ItemListener() {

			public void itemStateChanged(final ItemEvent e) {
				if (StringUtils.isEmpty(pluginNameLabel.getText())) {
					return;
				}

				String name = pluginNameLabel.getText();
				Core.setValueAt("plugin." + name + ".able", Boolean.toString(e
						.getStateChange() == ItemEvent.SELECTED));
				Core.save();
			}

		});

		// 好友添加按钮
		addButton.addActionListener(new ActionListener() {

			public void actionPerformed(final ActionEvent e) {
				String id = addBuddyField.getText();
				if (StringUtils.isNotEmpty(id) && StringUtils.contains(id, '@')) {
					String from = connection.getUser();
					if (StringUtils.contains(from, '/')) {
						from = StringUtils.substringBeforeLast(from, "/");
					}

					addBuddyField.setText("");
					int result = JOptionPane.showConfirmDialog(
							PreferenceWindow.this, "add buddy " + id + " ?",
							"Add buddy confirm", JOptionPane.OK_CANCEL_OPTION);
					if (result == JOptionPane.CANCEL_OPTION) {
						return;
					}

					Presence presence = new Presence(Presence.Type.subscribe);
					presence.setFrom(from);
					presence.setTo(id);
					connection.sendPacket(presence);
				}
			}

		});

		// 删除好友
		deleteButton.addActionListener(new ActionListener() {

			public void actionPerformed(final ActionEvent e) {
				if (deleteBuddyBox.getSelectedIndex() < 0) {
					return;
				}

				String id = (String) deleteBuddyBox.getSelectedItem();
				String from = connection.getUser();
				if (StringUtils.contains(from, '/')) {
					from = StringUtils.substringBeforeLast(from, "/");
				}

				int result = JOptionPane.showConfirmDialog(
						PreferenceWindow.this, "Delete buddy " + id + " ?",
						"Delete buddy confirm", JOptionPane.OK_CANCEL_OPTION);
				if (result == JOptionPane.CANCEL_OPTION) {
					return;
				}

				Presence presence = new Presence(Presence.Type.unsubscribe);
				presence.setFrom(from);
				presence.setTo(id);
				connection.sendPacket(presence);
			}

		});
	}

	// Variables declaration - do not modify
	private javax.swing.JTextField addBuddyField;

	private javax.swing.JButton addButton;

	private javax.swing.JComboBox deleteBuddyBox;

	private javax.swing.JButton deleteButton;

	private javax.swing.JButton closeButton;

	private javax.swing.JCheckBox pluginCheckBox;

	private javax.swing.JList pluginList;

	private javax.swing.JLabel pluginNameLabel;

	private javax.swing.JTextField userIdField;

	private XMPPConnection connection;

	private PluginManager pluginManager;

	// End of variables declaration

}
